/*
 ------------------------------------------------------------------------------
 Copyright (C) 2009-2011 Eternal Games.

 This file is part of the Eternal Tech source code.

 The Eternal Tech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Eternal Tech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Eternal Tech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// memory.h - Memory manager
//


#if !defined(__MEMORY_H__) || !INCLUDE_GUARDS
#define __MEMORY_H__


#define POOLS_HASH_SIZE					512

#define MAX_POOLS						64

struct memoryBlock_t {
	void					*pointer;					// Pointer to allocated memory

	int 					size;						// Size minus the header, sentinel, and any rounding up to the byte barrier
	int						realSize;					// Actual size of block

	struct memPuddle_s		*puddle;					// Puddle allocated from

	struct memoryPool_t		*pool;

	byte					topSentinel;				// For memory integrity checking

	struct memoryBlock_t	*prev;						// Previous block in this pool
	struct memoryBlock_t	*next;						// Next block in this pool
};

struct memoryPool_t {
	char					name[MAX_PATH_LENGTH];

	int						numBlocks;					// Total allocated blocks
	int						byteCount;					// Total allocated bytes

	memoryBlock_t			blockHeadNode;				// Allocated blocks

	struct memoryPool_t		*nextHash;
};

#define _Mem_Alloc(size) Mem_Alloc(size, com_genericMemoryPool)

// Find or creates a memory pool
memoryPool_t		*Mem_FindPool (const char *name);

// Registers a memory pool
memoryPool_t		*Mem_RegisterPool (const char *name);

// Allocates a block of memory of the given size
void				*Mem_Alloc (int size, memoryPool_t *pool);

// Frees a block of memory
void				Mem_Free (const void *ptr);

// Frees a memory pool
void				Mem_FreePool (memoryPool_t *pool);

// Allocates a block of memory and copies the given string
char				*Mem_DupString (const char *string, memoryPool_t *pool);

// Allocates a block of memory and copies the given data
void				*Mem_DupData (const void *data, int size, memoryPool_t *pool);

// Initializes the memory manager
void				Mem_Init ();

// Shuts down the memory manager
void				Mem_Shutdown ();


#else
FILE_WARNING
#endif   // __MEMORY_H__